home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1833
/
1833.xpi
/
chrome
/
yoono.jar
/
content
/
yoono
/
utils.js
< prev
next >
Wrap
Text File
|
2009-12-16
|
10KB
|
319 lines
// max number of similar sites possible to request at a time
const MAXRECOREQUESTS = 50;
// max number of experts possible to request at a time
const MAXEXPERTREQUESTS = 30;
// max number of articles
const MAXARTICLEREQUESTS = 30;
const YOONO_CI = Components.interfaces;
const YOONO_CL = Components.classes;
const YNPREFBRANCH = YOONO_CL["@mozilla.org/preferences-service;1"].getService(YOONO_CI.nsIPrefService).getBranch("extensions.yoono.");
// Yoono services
try {
Components.utils.import("resource://yoono/yoonoYEXTIF.js");
Components.utils.import("resource://yoono/yoonoService.js");
Components.utils.import("resource://yoono/yoonoBkmSync.js");
Components.utils.import("resource://yoono/yoonoLog.js");
Components.utils.import("resource://yoono/yoonoDialogs.js");
Components.utils.import("resource://yoono/yoonoUtils.js");
Components.utils.import("resource://yoono/yoonoServerConnection.js");
Components.utils.import("resource://yoono/yoonoPrefs.js");
Components.utils.import("resource://yoono/yoonoSidebarService.js");
Components.utils.import("resource://yoono/yoonoStorage.js");
Components.utils.import("resource://yoono/yoonoKeyValueDB.js");
} catch(e) {
Components.utils.reportError(e);
}
String.prototype.beginsWith = function (substr) {
return (this.slice(0, substr.length) == substr);
}
/*
renvoie un objet xml repr�sentant le noeud <context> pour
une requete xmlhttprequest envoy�e au serveur
*/
function scriptContext() {
var userid = "";
var syncid = "";
try {
userid = YNPREFBRANCH.getCharPref('userid');
syncid = YNPREFBRANCH.getCharPref('syncid');
} catch(e) {
// nothing to do.
}
var context = <context>
{(userid ? <user-id> {userid} </user-id> : '')}
{(syncid ? <sync-id> {syncid} </sync-id> : '')}
<version>{YOONO_UTILS.getExtensionVersion()}</version>
<client>xpi</client>
</context>;
return context;
}
/*
str commence par referenceStr ?
*/
function beginWith (str, referenceStr) {
if (str.slice(0, referenceStr.length) == referenceStr) {
return true;
} else {
return false;
}
}
function getHostFromUrl(url) {
var host = url.match( /:\/\/(www\.)?([^\/:]+)/ );
if (host && host[2]) {
host = host[2]?host[2]:'';
} else {
host = '';
}
return host;
}
// rend invisible tous les elements <description>
// qui ont pour class : "warn"
function clearWarnings() {
var className;
var listdesc = document.getElementsByTagName("description");
for (var i = listdesc.length ; i-- > 0 ; ) {
className = listdesc[i].className;
if (className == 'warning') {
listdesc[i].hidden = true;
}
}
}
// disable all textbox elements
function disableTextBoxes() {
var listTextBox = document.getElementsByTagName("textbox");
for (var i = listTextBox.length ; i-- > 0 ; ) {
listTextBox[i].readonly = true;
}
}
// valide les entrees des formulaires de login
function validateLoginInput(pwd) {
// read pwd and pwd confirmation getting rid of default value if left untouched by user
var valid = true;
if (pwd.length < 6) {
valid = false;
}
if (pwd.length > 20) {
valid = false;
}
loginValidationRegExp = new RegExp(/[^a-zA-Z0-9.\-_]/);
if (loginValidationRegExp.test(pwd)) {
valid = false;
}
return(valid);
}
function isRegisteredUser (id) {
if (!id) return false;
if (id.indexOf(':') == -1) {
return false;
} else {
return true;
}
}
function centerWindow () {
sizeToContent();
window.screenX = (window.screen.width - window.outerWidth) / 2;
window.screenY = (window.screen.height - window.outerHeight) / 2;
}
var ynRecoTooltip = {
create: function() {
var tooltip = document.createElement("tooltip");
return tooltip;
},
show: function(tooltip, title, rank, url) {
if (tooltip.getAttribute('alreadyshown'))
return;
tooltip.setAttribute('alreadyshown', 'true');
tooltip.setAttribute('class', 'recotooltip');
tooltip.setAttribute('noautohide', 'true');
/* container */
var maingrid = this.createGrid(2);
// thumbnail
var thumburi = 'http://open.thumbshots.org/image.pxf?url=' + encodeURIComponent(url);
var row = document.createElement('row');
var img = document.createElement('image');
img.setAttribute('src', thumburi);
img.setAttribute('onload', "ynRecoTooltip.onLoadThumbnail('" + url + "',this)");
row.appendChild(img);
/* secondarygrid */
var secondarygrid = this.createGrid(1);
// same row element as for <image/>
row.appendChild(secondarygrid);
maingrid.childNodes[1].appendChild(row);
/* title and favicon */
row = document.createElement('row');
row.setAttribute('class', 'bold');
var box = document.createElement('box');
row.appendChild(box);
var uri = YOONO_CL['@mozilla.org/docshell/urifixup;1'].
getService(YOONO_CI.nsIURIFixup).createFixupURI(url,0);
var faviconuri = uri.scheme + '://' + uri.asciiHost + '/favicon.ico';
var img = document.createElement('image');
img = document.createElement('image');
img.setAttribute('src', faviconuri);
img.setAttribute('class', 'tooltipicon');
box.appendChild(img);
var label = document.createElement('label');
label.setAttribute('value', sliceText(title, 45));
box.appendChild(label);
secondarygrid.childNodes[1].appendChild(row);
/* url */
row = this.rowlabel(sliceText(url, 45));
row.setAttribute('class', 'recotooltipurllabel');
secondarygrid.childNodes[1].appendChild(row);
/* rank */
row = this.rowlabel(yoonoGlob.gStrbundle.getString('autosuggest.rank') + rank);
secondarygrid.childNodes[1].appendChild(row);
/* notice : mp pr�sent dans les mps */
if (tooltip.getAttribute("knownurl")) {
var row = document.createElement('row');
row.setAttribute('class', 'knownurlnotice');
var box = document.createElement('box');
img = document.createElement("image");
img.setAttribute("src", "chrome://yoono/skin/knownurl-coche.png");
img.setAttribute("class", "tooltipicon");
box.appendChild(img);
var label = document.createElement("label");
label.setAttribute("value", yoonoGlob.gStrbundle.getString("knownurl.notice"));
box.appendChild(label);
row.appendChild(box);
secondarygrid.childNodes[1].appendChild(row);
}
/* thumbshots.com notice */
row = this.rowlabel('http://www.thumbshots.com');
// will be displayed only if thumbnail exists
row.setAttribute('class', 'nodisplay');
secondarygrid.childNodes[1].appendChild(row);
tooltip.appendChild(maingrid);
return;
},
onLoadThumbnail: function(url,image) {
if (image.loaded) return;
var width = document.defaultView.getComputedStyle(image, null).getPropertyValue("width");
if (width != '1px' && width != '2px') {
// thumbnail exists
image.setAttribute('class', 'imgthumbshot');
var notice = image.nextSibling.childNodes[1].lastChild;
notice.setAttribute('class', 'thumbshotnotice');
image.loaded = true;
} else {
var uri = YOONO_CL['@mozilla.org/docshell/urifixup;1'].
getService(YOONO_CI.nsIURIFixup).createFixupURI(url,0);
var hosturl = uri.scheme + '://' + uri.host;
if (hosturl == url) {
image.loaded = true;
} else {
var thumburi = 'http://open.thumbshots.org/image.pxf?url=' + encodeURIComponent(hosturl);
image.setAttribute('src', thumburi);
image.setAttribute('onload', "ynRecoTooltip.onLoadThumbnail('" + hosturl + "',this)");
}
}
},
rowlabel: function(text) {
var row = document.createElement('row');
var label = document.createElement('label');
label.setAttribute('value', text);
row.appendChild(label);
return row;
},
createGrid: function(nbcols) {
var grid = document.createElement('grid');
var columns = document.createElement('columns');
var rows = document.createElement('rows');
for (var i = 0 ; i < nbcols; i++) {
var column = document.createElement('column');
columns.appendChild(column);
}
grid.appendChild(columns);
grid.appendChild(rows);
return grid;
}
}
function ynAllBrowserWindow () {
var WMED = YOONO_CL['@mozilla.org/appshell/window-mediator;1'].getService(YOONO_CI.nsIWindowMediator);
return WMED.getEnumerator("navigator:browser");
}
function getCurrentPageHref() {
return getBrowser().contentDocument.location.href;
}
function sliceText(aText, aMaxChar) {
return (aText.length > aMaxChar) ? aText.slice(0,aMaxChar) + ' \u2026' : aText;
}
var hasSomeItems = function(obj) {
for (var i in obj) {
return true;
}
return false;
}
/* outils de d�buggage */
const YOONO_CONSOLESERVICE = YOONO_CL["@mozilla.org/consoleservice;1"].getService(YOONO_CI.nsIConsoleService);
var log = function(message) {
YOONO_CONSOLESERVICE.logStringMessage("Yoono : " + message );
if('stack' in message)
YOONO_CONSOLESERVICE.logStringMessage("Yoono : " + message.stack );
}
var Assert = function (condition) {
if (!condition) {
throw ("assert detected in : " + Assert.caller);
}
}